package com.bootdo.app;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.IntegralLogDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.IntegralLogService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app/integralLog")
public class AppIntegralLogController extends BaseController {

    @Autowired
    private IntegralLogService logService;

    @Autowired
    private UserRoleDao userRoleDao;

    /**
     * 获取赠送积分列表数据
     * @param params
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    public R integralLogList(@RequestParam Map<String, Object> params, HttpServletRequest request){
        R r = checkToken(request);
        if(!StringUtils.equals(String.valueOf(r.get("code")),"0")) {
            return r;
        }
        //用户信息
        UserDO userDO = (UserDO) r.get("data");
        //查询列表数据
        //获取用户角色
        List<Long> list = userRoleDao.listRoleId(userDO.getUserId());
        if(!CollectionUtils.isEmpty(list)) {
            if(!list.contains(1l)) {
                //普通用户
                params.put("userId",userDO.getUserId());
            }
        }
        Query query = new Query(params);
        List<IntegralLogDO> logList = logService.list(query);
        int total = logService.count(query);
        PageUtils pageUtils = new PageUtils(logList, total);
        return R.ok(pageUtils);
    }

    /**
     * 保存赠送积分
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(IntegralLogDO log, HttpServletRequest request){
        R r = checkToken(request);
        if(!StringUtils.equals(String.valueOf(r.get("code")),"0")) {
            return r;
        }
        r = logService.save(log);
        return r;
    }

}
